//
//  nativeProductTableViewController.m
//  SXH
//
//  Created by yangli on 15/7/9.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "NativeProductTableViewController.h"
#import "CommonService.h"
#import "LPTableViewCell.h"
#import "UIImageView+WebCache.h"

#import "NativeTableViewCell.h"

@interface NativeProductTableViewController ()

@end

@implementation NativeProductTableViewController

- (instancetype)initWithID:(NSString *)aID andEntity:(LPEntity *)aEntity
{
    self = [super init];
    if (self) {
        self.myID = aID ;
        self.myEntity = aEntity;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    /*
    NSError *error;
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    //NSString *urlString = [NSString stringWithFormat:@"%@/api/heartno",@"http://115.28.239.93"];
    
    NSString *urlString = [NSString stringWithFormat:@"%@/specialty/queryById",BASEURL];
    
    NSLog(@"%@",urlString);
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];
    
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    [request setHTTPMethod:@"POST"];
    //NSDictionary *mapData = @{@"consultant":@"李丹"};
    NSDictionary *mapData = @{@"id":self.myID};
    NSLog(@"%@",mapData);
    NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
    [request setHTTPBody:postData];
    
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@", json);
        
        
    }];
    
    [postDataTask resume];
    */
    
    
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = @{@"id":self.myID};
    NSString *urlString = [NSString stringWithFormat:@"%@/specialty/queryById",BASEURL];
    
    [service getNetWorkData:param
                       Path:urlString
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *) entity;
                      NSLog(@"%@",dic);
                      self.myDic = entity;
                      
                      dispatch_async(dispatch_get_main_queue(), ^{
                          [self.tableView reloadData];
                      });
                      
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      DebugLog(@"%@", message);
                  }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        static NSString *couponTableViewCellIdentifier=@"LPTableViewCell";
        [tableView registerNib:[UINib nibWithNibName:@"LPTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:couponTableViewCellIdentifier];
        LPTableViewCell *cell = (LPTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
        
        
        
        NSString *url = [NSString stringWithFormat:@"%@/%@", IMAGE_URL, self.myEntity.album];
        [cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:url]];
        cell.labelName.text= self.myEntity.title;
        
        return cell;
    }
    else{
        [tableView registerClass:[NativeTableViewCell class] forCellReuseIdentifier:@"NativeTableViewCell"];
        NativeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NativeTableViewCell" forIndexPath:indexPath];
        //nativeTableViewCell *cell = [[nativeTableViewCell alloc] init];
        
        if (self.myDic) {
            cell.myText.text = [[self.myDic objectForKey:@"row"] objectForKey:@"content"];
        }
        
        return cell ;
        
    }
}

 - (CGSize)screenSize {
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        return CGSizeMake(screenSize.height, screenSize.width);
    }
    return screenSize;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 158;
    }
    else{
        
        return [self screenSize].height-158;
        
    }
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
